// import { buildColumns } from '@/utils/cdn'

export default function useTableColumn() {
  const columns = computed(() =>
    buildColumns([
      {
        title: '文件后缀',
        dataIndex: 'suffix',
        slotName: 'suffix',
        fixed: 'left',
      },
      {
        title: '缓存时间',
        dataIndex: 'cache_time',
        slotName: 'cache_time',
        width: 200,
      },
      {
        title: '操作',
        width: 120,
        slotName: 'action',
      },
    ]),
  )
  const TimeUnitConversion = (time: string) => {
    if (time) {
      const unit = time.slice(-1) // 取最后一个字符
      const val = time.slice(0, -1) // 取除最后一个字符外的其余字符
      return `${val} ${unit === 's' ? '秒' : unit === 'm' ? '分钟' : unit === 'h' ? '小时' : unit === 'd' ? '天' : '秒'}`
    } else {
      return '-'
    }
  }
  const byteConvertUnit = (bytes: number, unit = 'B') => {
    if ((bytes || bytes !== 0) && !Number.isNaN(bytes)) {
      let bits: number | string = bytes
      //   let bits: number | string = bytes * 8
      const symbols
        = unit === 'B'
          ? ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB']
          : [
              'bps',
              'Kbps',
              'Mbps',
              'Gbps',
              'Tbps',
              'Pbps',
              'Ebps',
              'Zbps',
              'Ybps',
              'Bbps',
            ]
      let exp = Math.floor(Math.log(bits) / Math.log(2))
      if (exp < 1) {
        exp = 0
      }
      const i = Math.floor(exp / 10)

      bits /= 2 ** (10 * i)
      if (bits.toString().length > bits.toFixed(2).toString().length) {
        bits = bits.toFixed(2)
      }
      if (Number.isNaN(bits) && !symbols[i]) {
        return {
          value: 0,
          unit: '',
        }
      }
      return {
        value: bits,
        unit: symbols[i],
      }
    }
    return {
      value: 0,
      unit: unit === 'B' ? 'B' : 'bps',
    }
  }
  const staCodeNumberFormat = (value: number | 0) => {
    const param = {
      value: '0',
      unit: ' ',
    }
    const k = 10000
    const sizes = ['次', '万次', '亿次', '万亿次']
    let i

    if (value < k) {
      param.value = value % 1 === 0 ? String(value) : value.toFixed(0)
      param.unit = '次'
    } else {
      i = Math.floor(Math.log(value) / Math.log(k))
      param.value = (value / k ** i).toFixed(2)
      param.unit = sizes[i]
    }
    return { ...param }
  }
  return {
    columns,
    TimeUnitConversion,
    byteConvertUnit,
    staCodeNumberFormat,
  }
}
